Scripting for transform operation

Hello, I am trying to render the files in the folder, and it works well.

How can I apply transformations, such as rotation or scaling, when importing by script? Could anyone provide a script example?

Hello liang Yang, below is an example script which will import an OBJ file from a directory and apply transformations. You’ll need to adjust it per your requirements.

AUTHOR LUX DT ChatGPT

VERSION 0.2.2

Headless Example: Import OBJ files from a directory and apply transformations

import lux
import luxmath
import os

def import_obj_files_from_directory(directory):
    """
    Import OBJ files from the specified directory into KeyShot.
    """
    for filename in os.listdir(directory):
        if filename.lower().endswith('.obj'):
            file_path = os.path.join(directory, filename)
            print(f"Importing OBJ file: {file_path}")
            lux.importFile(file_path, showOpts=False)

def apply_transformations_to_all_models(distance, scale_factor):
    """
    Apply translation, rotation, and scaling transformations to all models in the scene.
    """
    scene_root = lux.getSceneTree()
    model_nodes = scene_root.find("", types=[lux.NODE_TYPE_OBJECT])

    for model_node in model_nodes:
        print(f"Transforming model node: {model_node.getName()}")

        # Translation
        translation_vector = luxmath.Vector(0, -distance, 0)
        translation_matrix = luxmath.Matrix.makeTranslate(translation_vector)

        # Rotation
        rotation_axis = luxmath.Vector(0, 0, 1)
        rotation_angle = -90
        rotation_matrix = luxmath.Matrix().makeIdentity().rotateAroundAxis(rotation_axis, rotation_angle)

        # Scaling
        scale_matrix = luxmath.Matrix().makeIdentity().scale(luxmath.Vector(scale_factor, scale_factor, scale_factor))

        # Combining transformations
        combined_transform = translation_matrix.mul(rotation_matrix).mul(scale_matrix)

        model_node.applyTransform(combined_transform, absolute=False)

# Specify the directory containing OBJ files
directory = r"C:\GLB_OBJ"

# Import OBJ files from the directory
import_obj_files_from_directory(directory)

# Apply transformations to all imported models
apply_transformations_to_all_models(2, 2)

print("Importing and transformations of OBJ files complete.")

Try this if you need further scripting assistance.